import {fileURLToPath, URL} from 'node:url'

import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig(({mode, _command}) => {
    console.log('mode:', mode)
    const env = loadEnv(mode, process.cwd());
    const {VITE_TARGET_URL} = env;
    return {
        plugins: [vue()],
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: `@import "./src/assets/css/bem.scss";`
                }
            }
        },
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url))
            }
        },
        server: {
            host: '0.0.0.0',
            port: 10010,
            https: false,
            proxy: {
                '/api': {
                    target: VITE_TARGET_URL,  //你要跨域访问的网址
                    changeOrigin: true,   // 允许跨域
                    rewrite: (path) => path.replace(/^\/api/, '') // 重写路径把路径变成空字符
                }
            }
        }
    }
})
